package org.jeecg.modules.system.test;

import org.jeecg.common.lineparser.LineParser;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.atomic.AtomicInteger;

public class Example {

  public static void main(String[] args) throws IOException {
    long t = System.currentTimeMillis();
    Path path = Paths.get("D:\\disk1\\xx.txt");
    Charset cs = StandardCharsets.UTF_8;
    LineParser parser = new LineParser(Integer.MAX_VALUE, 100000);
    AtomicInteger n = new AtomicInteger(0);
    try {
      parser.forEach(path, cs, (line) -> {
        System.out.printf("[%d,%d]%s%n", line.getOffset(), line.getLength(), line.getContent().subSequence(0,3));
        int andIncrement = n.getAndIncrement();
        if(andIncrement > 10){
          throw new RuntimeException("n=" + andIncrement);
        }

      });
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println(System.currentTimeMillis() - t);
    System.out.println(n.get());
//    t = System.currentTimeMillis();
//    RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\disk1\\xx.txt", "r");
//    int i = 0;
//    while(true){
//      String s = randomAccessFile.readLine();
//      if(s == null){
//        break;
//      }
//      System.out.printf("[%d,%d]%s%n", i, i, s.subSequence(0,3));
//      i ++;
//    }
//    System.out.println(System.currentTimeMillis() - t);
//    System.out.println(i);
  }

}
